<?php


namespace app\common\library;
use common;
use RequestContext;


class TimerALModel
{

    var $_timer_server_url = null;
    var $_sms_server_url = null;
    var $_bus_server_url = null;

    const SHELL = 'SHELL';
    const SMS = 'SMS';
    const URL = 'URL';

    static function instance()
    {
        static $_thiz = null;
        if (empty($_thiz)) {
            $_thiz = new TimerALModel();
            $_thiz->_timer_server_url = config('app.timer_server_url');
            $_thiz->_sms_server_url = config('app.sms_server_url');
            $_thiz->_bus_server_url = config('app.bus_server_url');
        }
        return $_thiz;
    }

    function call_bus_server($path, $params, $timeout)
    {
        $params['sn'] = RequestContext::$sn;
        $timer_data = [
            'delay' => $timeout,
            'TTR' => 30,
            'type' => self::URL,
            'body' => json_encode($params),
            'postUrl' => $this->_bus_server_url . $path,
            'maxRetry' => 0,
            'sn' => RequestContext::$sn,
        ];

        $url = $this->_timer_server_url . "/dq/push";
        $hs = [
            'token: kevin_cdl',
            'host: service.dq'
        ];

        common::curlpostjson($url, $timer_data, $hs);
    }

    function send_sms_user_ride_violation($phone)
    {

        $sms_data = [
            'outId' => RequestContext::$sn,
            'phone' => $phone,
            'code' => SMS_TPL_USER_RIDE_VIOLATION,
            'extendCode' => '',
            'params' => '',
        ];

        $timer_data = [
            'delay' => 0,
            'TTR' => 10,
            'type' => self::SMS,
            'body' => json_encode($sms_data),
            'postUrl' => $this->_sms_server_url . '/sms/send',
            'maxRetry' => 0,
            'sn' => RequestContext::$sn,
            'headers' => json_encode(
                [
                    'token' => 'kevin_cdl',
                    'host' => 'service.sms'
                ]
            ),
        ];

        $url = $this->_timer_server_url . "/dq/push";
        $hs = [
            'token: kevin_cdl',
            'host: service.dq'
        ];

        common::curlpostjson($url, $timer_data, $hs);
    }
}
